package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class SpringCacheSyncHandler {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IServeService serveService;

    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync(){

        //定时更新开通区域列表缓存

        //删除当前缓存中 开通区域列表
        redisTemplate.delete(RedisConstants.CacheName.JZ_CACHE+"::ACTIVE_REGIONS");
        //重新将开通区域列表添加到缓存中
        List<RegionSimpleResDTO> dtos = regionService.queryActiveRegionListCache();
        //定时更新首页服务列表缓存（首页服务按地区来）
        for (RegionSimpleResDTO dto : dtos) {

            Long id = dto.getId();
            //删除当前缓存中 首页服务列表
            redisTemplate.delete(RedisConstants.CacheName.SERVE_ICON+"::"+id);
            //删除当前缓存中 热门服务列表
            redisTemplate.delete(RedisConstants.CacheName.HOT_SERVE+"::"+id);
            //重新将首页服务列表添加到缓存中
            serveService.firstPageServeList(id);
            //重新将热门服务列表添加到缓存中
            serveService.hotServeList(id);

        }
    }
}
